package com.xbongbong.crm.service.permission;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.abstracts.AbstractBusinessPermissionService;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.permission.pojo.dto.VerifyViewPermissionDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.CommunicatePlanUserModel;
import com.xbongbong.sys.model.UserModel;
import com.xbongbong.sys.util.UserPermissionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sunshaoxiang
 * @ClassName CommunicatePlanPermissionServiceImpl
 * @Description 访客计划权限 Service
 * @Date 2022/6/23 10:59
 * @Version 1.0
 **/
@Service
public class CommunicatePlanPermissionServiceImpl extends AbstractBusinessPermissionService<PaasFormDataEntityExt, PaasFormEntityExt> {
    @Resource
    private UserModel userModel;
    @Resource
    private CommunicatePlanUserModel communicatePlanUserModel;

    @Override
    public List<Integer> businessType() {
        return Collections.singletonList(XbbRefTypeEnum.COMMUNICATE_PLAN.getCode());
    }



    @Override
    public void checkPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {
        VerifyViewPermissionDTO verifyViewPermissionDTO = new VerifyViewPermissionDTO(loginUser, paasFormDataEntityExt);
        //没有团队权限且没有共享抛异常
        boolean hasDataPermission = hasPermission(formEntity, paasFormDataEntityExt, loginUser, subBusinessType)
                || sharePermissionHelp.verifySharePermission4QueryWithRuleEngine(verifyViewPermissionDTO);
        if (!hasDataPermission) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100017);
        }
    }

    @Override
    public boolean hasPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {
        if (UserPermissionUtil.isMaxDataPermission(loginUser, formEntity.getSaasMark(), formEntity.getBusinessType(), formEntity.getDistributorMark(), formEntity.getMenuId(), false)){
            return true;
        }
        Long dataId = paasFormDataEntityExt.getId();
        Integer dataPermission = userModel.getDataPermission(loginUser, false);
        List<String> userIdIn = userModel.getSubIdListByDataPermission(dataPermission, loginUser.getUserId(), loginUser.getCorpid(), BasicConstant.ZERO,false);
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        String corpid = formEntity.getCorpid();
        param.put("corpid",corpid);
        param.put("dataId",dataId);
        param.put("userIdIn",userIdIn);
        param.put("del",0);
        Integer count = communicatePlanUserModel.getEntitysCount(param);
        return count > 0;
    }
}
